package com.nfit.shopadmin.controller;

import com.nfit.shopadmin.service.AddressService;
import com.nfit.shopmodel.dto.address.AddressDto;
import com.nfit.shopmodel.vo.address.AddressVo;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.service.RequestBodyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:AddressController
 * Package:com.nfit.shopadmin.controller
 * Description
 *
 * @Author L
 * @Create 2024/12/29 1:36
 */
@Tag(name = "地址管理")
@Slf4j
@RestController
@RequestMapping("/admin/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据userId查询地址列表
     */
    @GetMapping("/findByUserId/{userId}")
    public Result findByUserId(@PathVariable("userId") Long userId){
        List<AddressVo> list = addressService.findByUserId(userId);
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据ID查询地址详情信息
     */
    @GetMapping("/findById/{id}")
    private Result findById(@PathVariable("id") Long id){
        AddressVo addressVo = addressService.findById(id);
        return Result.build(addressVo,ResultCodeEnum.SUCCESS);
    }

    /**
     *添加地址
     */
    @PostMapping("/save")
    private Result save(@RequestBody AddressDto addressDto){
        addressService.save(addressDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     *修改地址
     */
    @PutMapping("/update/{id}")
    private Result update(@RequestBody AddressDto addressDto,
                          @PathVariable("id") Long id){
        addressService.update(addressDto,id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    private Result delete(@PathVariable("id") Long id){
        addressService.delete(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
